package com.app.teaware.common;

import android.app.Activity;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.alibaba.fastjson.JSONArray;
import com.app.teaware.R;
import com.app.teaware.basis.BasisApp;
import com.app.teaware.bean.UploadImgData;
import com.app.teaware.bean.UserUploadFileBean;
import com.app.teaware.other.PhotoScanDialog;
import com.app.teaware.util.MyUtils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdapterImgEdit extends RecyclerView.Adapter<AdapterImgEdit.ViewHolder> {

        public ArrayList<UploadImgData> mListBean = new ArrayList<>();
        Activity mContext;
        boolean isEdit = false;
        int MAX_SIZE = 6;
        private int REQUEST_CODE = 99;

        public AdapterImgEdit(Activity mContext,ArrayList<UploadImgData> list) {
            mListBean = list;
            this.isEdit = false;
            this.mContext = mContext;
        }
        public AdapterImgEdit(Activity mContext,ArrayList<UploadImgData> list,int maxSize,int request_code) {
            mListBean = list;
            if(mListBean == null){
                mListBean = new ArrayList<>();
            }
            this.isEdit = true;
            this.mContext = mContext;
            this.MAX_SIZE = maxSize;
            this.REQUEST_CODE = request_code;
        }
//
        public String getParamsImgidsStr(){
            if(mListBean == null || mListBean.size() == 0)return "";
//            String[] ids = new String[mListBean.size()];
            StringBuilder builder = new StringBuilder();
//            JSONArray jsonArray = new JSONArray();
            for(int i = 0 , l = mListBean.size() ; i < l ; i++){
//                ids[i] = mListBean.get(i).getId();
//                jsonArray.add(mListBean.get(i).getId());
                builder.append(mListBean.get(i).getId()).append(";");
            }
            if(builder.length() > 0){
                builder.deleteCharAt(builder.length()-1);
            }
//            return jsonArray.toJSONString();
            return  builder.toString();
        }

    public JSONArray getParamsImgidArray(){
        if(mListBean == null || mListBean.size() == 0)return  null;
//            String[] ids = new String[mListBean.size()];
            JSONArray jsonArray = new JSONArray();
        for(int i = 0 , l = mListBean.size() ; i < l ; i++){
                jsonArray.add(mListBean.get(i).getId());
        }
        return  jsonArray;
    }

    public static JSONArray getParamsImgidArray(ArrayList<UserUploadFileBean> list){
        if(list == null || list.size() == 0)return  null;
//            String[] ids = new String[mListBean.size()];
        JSONArray jsonArray = new JSONArray();
        for(int i = 0 , l = list.size() ; i < l ; i++){
            jsonArray.add(list.get(i).getId());
        }
        return  jsonArray;
    }

        public void setData(ArrayList<UploadImgData> list) {
            mListBean = list;
            if(mListBean == null){
                mListBean = new ArrayList<>();
            }
            notifyDataSetChanged();
        }

        public void setImgId(int position,String imgId){
            UploadImgData imgData = mListBean.get(position);
            imgData.setId(imgId);
            mListBean.set(position,imgData);
        }

    public void addImg(  ArrayList<ImageItem> images){
        if(images == null || images.size() == 0)return;
            for (int i = 0, l = images.size(); i < l; i++) {
                mListBean.add(new UploadImgData(images.get(i)));
            }
        notifyDataSetChanged();
    }

        //创建新View，被LayoutManager所调用
        @Override
        public AdapterImgEdit.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_img_item, viewGroup, false);
            return new AdapterImgEdit.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(AdapterImgEdit.ViewHolder viewHolder, int position) {
          viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            if(mListBean == null)return 0;
            if (isEdit) {
                if (mListBean.size() >= MAX_SIZE)
                    return MAX_SIZE;
                return mListBean.size() + 1;
            } else {
                return mListBean.size();
            }

        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.img_icon)
            public ImageView img_icon;
            @BindView(R.id.img_delete)
            public ImageView img_delete;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
            public void bind(int position){
                if (isEdit && mListBean.size() < MAX_SIZE && position == mListBean.size()) {
//                Glide.with(mContext).load(R.drawable.img_add).into(viewHolder.img_icon);
                    img_icon.setImageResource(R.drawable.base_img_photo_btn_upload);
                    img_delete.setVisibility(View.INVISIBLE);
                } else {
                    UploadImgData data = mListBean.get(position);
                   list_item.setTag(data);
                    BasisApp.loadImg(mContext,data.getPath(),img_icon,R.drawable.base_img_default_square);
                   img_delete.setVisibility(isEdit ? View.VISIBLE : View.INVISIBLE);
                    img_delete.setTag(position);
//            viewHolder.list_item.setBackgroundResource(R.drawable.item_shadow_bluelight_selector);
                }


               img_icon.setTag(R.id.img_icon, position);
            }

            @OnClick(R.id.img_icon)
            public void onItemClick(View v) {
                int position = (int) v.getTag(R.id.img_icon);
                if (isEdit && position == mListBean.size()) {
                    ImagePicker.getInstance().setMultiMode(true);
                    ImagePicker.getInstance().setSelectLimit(MAX_SIZE - mListBean.size());
                    Intent intent = new Intent(mContext, ImageGridActivity.class);
                    mContext.startActivityForResult(intent, REQUEST_CODE);
                }else {
                    PhotoScanDialog.toImgScanDialog(mContext, MyUtils.getImageFromImgData(mListBean),position);
                }
            }

            @OnClick(R.id.img_delete)
            public void onItemDelete(View v) {
                int position = (int) v.getTag();
//                UploadImgData data = mListBean.get(position);
                mListBean.remove(position);
               notifyDataSetChanged();
            }
        }
    }